package com.jhws.business.card;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.business.card.bo.CardConstant;
import com.jhws.business.card.bo.TblCardRecord;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;
import com.jhws.core.exception.BizException;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.owner.service.ITblOwnerDimService;
import com.jhws.util.ObjectUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TblCardRecordServiceImpl {
    @Resource
    private BaseDao BaseDao;
    @Resource
    private ITblOwnerDimService IOwnerService;
    @Resource
    private ITblCommDimService iTblCommDimService;
    @Resource
    private OSSClientService ossClientService;

    public void save(TblCardRecord bo) {
        // TODO Auto-generated method stub
        BaseDao.saveOrUpdate(bo);
    }

    public void delete(String id) {
        // TODO Auto-generated method stub
        BaseDao.deleteByWhere("TblCardRecord", "id = '" + id + "'");
    }

    /**
     * 查询刷卡列表
     *
     * @param param
     * @return
     * @throws Exception
     */
    @SuppressWarnings("Duplicates")
    public Map<String, Object> findBrushCardList(Map param) throws Exception {
        String whereClause = "1=1 ";

        String startDate = MapUtils.getString(param, "startDate", "");
        String endDate = MapUtils.getString(param, "endDate", "");

        String comm = MapUtils.getString(param, "commCode", "");
        if (StringUtils.isNotEmpty(comm)) {
            whereClause += " and commCode = '" + comm + "'";
            String roomInfo = MapUtils.getString(param, "roomInfo", "");
            if (StringUtils.isNotEmpty(roomInfo)) {
                whereClause += " and roomName like '" + comm + roomInfo + "%'";
            }
        }

        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            whereClause += " and swipeCardTime >= '" + startDate + " 00:00:00' and swipeCardTime <='" + endDate + " 23:59:59'";
        } else {
            throw new BizException("日期错误！");
        }

        String ownerName = MapUtils.getString(param, "ownerName", "");
        if (param.containsKey("ownerName") && !ownerName.equals("")) {
            whereClause += " and ownerName like '%" + param.get("ownerName") + "%'";
        }

        String state = MapUtils.getString(param, "state", "");
        if (StringUtils.isNotEmpty(state)) {
            whereClause += " and state = '" + state + "'";
        }

        String cardType = MapUtils.getString(param, "cardType", "");
        if (StringUtils.isNotEmpty(cardType)) {
            whereClause += " and cardType = '" + cardType + "'";
        }

        String sql = "select * from card_record where " + whereClause + " order by swipeCardTime desc";

        Map<String, Object> result = new HashMap<String, Object>();
        Page p = BaseDao.queryPage(sql, Map.class);
        List<Map<String, Object>> list = p.getList();
        for (Map<String, Object> map : list) {
            String type = (String) map.get("cardType");
            if (type.equals(CardConstant.CARD_TYPE_NORMAL)) {
                String ownerCode = (String) map.get("ownerCode");
                TblOwnerDimBo bo = IOwnerService.findTblOwnerDimById(ownerCode);
                if (bo != null)
                    map.put("roomInfo", bo.getRoomInfo());
            } else if (type.equals(CardConstant.CARD_TYPE_SUPPER)) {
                String commCode = (String) map.get("commCode");
                TblCommDimBo tblCommDimBo = iTblCommDimService.findTblCommDimByCommCode(commCode);
                map.put("ownerName", "管理员");
                if (tblCommDimBo != null) {
                    map.put("roomInfo", "[" + tblCommDimBo.getCommName() + "]");
                }
            }
            String imageUrl = (String) map.get("imageUrl");
            String url = ossClientService.getVisitorUrl(imageUrl);
            if (!StringUtils.isEmpty(url) && !url.equals(imageUrl)) {
                map.replace("imageUrl", url);
            }
        }
        result.put("list", p.getList());
        result.put("size", p.getTotalNumber());
        result.put("ret_code", 1);
        return result;
    }
}
